<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Permission extends Gio_Core_Controller
{
	public function indexAction()
	{
		$request = $this->getRequest();
		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$mod = $request->getPost('mod');
			$controllers = Modules_Core_Services_Module::getActions($mod);
			$this->view->controllers = $controllers;
			
			$dbControllers = Modules_Core_Services_Controller::dbControllers($mod);
			$installedControllers = array();
			if ($dbControllers) {
				foreach ($dbControllers as $controller) {
					$installedControllers[] = $controller['controller_id'];
				}
			}
			$this->view->installedControllers = $installedControllers;
			
			$viewFile = ROOT_DIR . DS . 'templates' . DS . $this->view->APP_TEMPLATE
							. DS . 'modules' . DS . 'core' . DS . 'permission' . DS . 'module.ajax.phtml';
			$response = $this->view->render($viewFile);
			$this->getResponse()->setBody($response);
			return;
		}
		
		$modules = Modules_Core_Services_Module::getModulesInstalled();
		/**
		 * Add module core to the top array 
		 */
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'core'));
		$this->view->modules = $modules;
	}
	
	public function userAction()
	{
		$request = $this->getRequest();
		$userId = $request->getParam('user_id');
		$user = Modules_Core_Services_User::getById($userId);
		if (null == $user) {
			return;
		}
		$this->view->user = $user;

		$act = $request->getParam('act');

		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$mod = $request->getPost('mod');
			$this->view->mod = $mod;
			
			$controllers = Modules_Core_Services_Action::getDbActionsByModule($mod);
			
			switch ($act) {
				case 'save':	
					/**
					 *	Reset rule for user 
					 */			
					Modules_Core_Services_Rule::reset('user', $user['user_id'], $mod);
					$actions = $request->getPost('actions');
					if ($controllers) {
						foreach ($controllers as $index => $controller) {
							if ($controller['actions']) {
								foreach ($controller['actions'] as $indexAction => $controllerAction) {
									$allow = 0;
									if ($actions) {
										foreach ($actions as $action) {
											if ($mod . '_' . $controller['controller_id'] . '_' . $controllerAction['action_id']
													== $action) {
												$allow = 1;			
											}
										}
									}
									$rule = array(
										'module_id'		=> $mod,
										'controller_id'	=> $controller['controller_id'],
										'action_id'		=> $controllerAction['action_id'],
										'object_type'	=> 'user',
										'object_id'		=> $user['user_id'],
										'allow'			=> $allow,
									);
									$ruleId = Modules_Core_Services_Rule::add($rule);
								}
							}
						}
					}
					
					$this->getResponse()->setBody('RESULT_OK');
					return;
					break;
				default:
					$dbUserRules = Modules_Core_Services_Rule::getRules('user', $user['user_id']);
					$dbRoleRules = Modules_Core_Services_Rule::getRules('role', $user['role_id']);
					$dbRules = array_merge($dbUserRules, $dbRoleRules);
					$this->view->dbRules = $dbRules;
					
					$this->view->controllers = $controllers;
					
					$viewFile = ROOT_DIR . DS . 'templates' . DS . $this->view->APP_TEMPLATE
									. DS . 'modules' . DS . 'core' . DS . 'permission' . DS . 'user.ajax.phtml';
					$response = $this->view->render($viewFile);
					$this->getResponse()->setBody($response);
					return;
					break;
			}
		}
		
		$modules = Modules_Core_Services_Module::getModulesInstalled();
		/**
		 * Add module core to the top array 
		 */
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'core'));
		$this->view->modules = $modules;
	}
	
	public function roleAction()
	{
		$request = $this->getRequest();
		$roleId = $request->getParam('role_id');
		$role = Modules_Core_Services_Role::getById($roleId);
		if (null == $role) {
			return;
		}
		$this->view->role = $role;

		$act = $request->getParam('act');

		if ($request->isPost()) {
			$this->setNoRender();
			$this->disableLayout();
			
			$mod = $request->getPost('mod');
			$this->view->mod = $mod;
			
			$controllers = Modules_Core_Services_Action::getDbActionsByModule($mod);
			
			switch ($act) {
				case 'save':	
					/**
					 *	Reset rule for user 
					 */			
					Modules_Core_Services_Rule::reset('role', $role['role_id'], $mod);
					
					$actions = $request->getPost('actions');
					if ($controllers) {
						foreach ($controllers as $index => $controller) {
							if ($controller['actions']) {
								foreach ($controller['actions'] as $indexAction => $controllerAction) {
									$allow = 0;
									if ($actions) {
										foreach ($actions as $action) {
											if ($mod . '_' . $controller['controller_id'] . '_' . $controllerAction['action_id']
													== $action) {
												$allow = 1;			
											}
										}
									}
									$rule = array(
										'module_id'		=> $mod,
										'controller_id'	=> $controller['controller_id'],
										'action_id'		=> $controllerAction['action_id'],
										'object_type'	=> 'role',
										'object_id'		=> $role['role_id'],
										'allow'			=> $allow,
									);
									$ruleId = Modules_Core_Services_Rule::add($rule);
								}
							}
						}
					}
					
					$this->getResponse()->setBody('RESULT_OK');
					return;
					break;
				default:
					$dbRoleRules = Modules_Core_Services_Rule::getRules('role', $role['role_id']);
					$dbRules = $dbRoleRules;
					$this->view->dbRules = $dbRules;
					
					$this->view->controllers = $controllers;
					
					$viewFile = ROOT_DIR . DS . 'templates' . DS . $this->view->APP_TEMPLATE
									. DS . 'modules' . DS . 'core' . DS . 'permission' . DS . 'role.ajax.phtml';
					$response = $this->view->render($viewFile);
					$this->getResponse()->setBody($response);
					return;
					break;
			}
		}
		
		$modules = Modules_Core_Services_Module::getModulesInstalled();
		/**
		 * Add module core to the top array 
		 */
		$modules = array_pad($modules, -count($modules) - 1, array('module_id' => 'core'));
		$this->view->modules = $modules;
	}
}